package com.woorigil.social.controller.provider;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.woorigil.social.domain.provider.Provider;
import com.woorigil.social.domain.provider.ProviderDao;

@Controller
public class ProviderController {
	@Autowired ProviderDao providerDao;
	
	@RequestMapping("/admin/provider/registe")
	public ModelAndView registe(Provider provider){
		
		provider.setRegisteDate(new Date());
		
		if (provider.getCode() == null || provider.getCode() == "" ) {
			new IllegalArgumentException("아이디값이 없습니다");
			form(null);
		}else if(provider.getName() == null || provider.getName() == ""){
			new IllegalArgumentException("이름이 없습니다.");
			form(null);
		}else if(provider.getHomepage() == null || provider.getHomepage() == ""){
			new IllegalArgumentException("홈페이지가 없습니다.");
			form(null);
		}else if(provider.getTelephone()== null || provider.getTelephone() == ""){
			new IllegalArgumentException("전화번호가 없습니다.");
			form(null);
		}else if(provider.getRegisteDate()== null ){
			new IllegalArgumentException("날짜가 없습니다.");
			form(null);
		}
		
		//System.out.println(provider);
		providerDao.insert(provider);
		ModelAndView mav = new ModelAndView(new RedirectView("/admin/provider/list.html"));
		return mav;
	}
	
	@RequestMapping("/admin/provider/form")
	public ModelAndView form(String code) {
		Provider provider = null;
		if (code != null) {
			provider = providerDao.select(code);
		}
		
		ModelAndView mav = new ModelAndView("admin/provider/form");
		mav.addObject("provider", provider);
		return mav;
	}
	
	@RequestMapping("/admin/provider/modify")
	public ModelAndView modify(Provider provider){
		Provider originalProvider = providerDao.select(provider.getCode());
		
		if (originalProvider == null) {
			new IllegalArgumentException("값이 없습니다");
		}
		
		provider.setRegisteDate(originalProvider.getRegisteDate());
		providerDao.update(provider);
		
		ModelAndView mav = new ModelAndView(new RedirectView("/admin/provider/view.html"));
		mav.addObject("code", provider.getCode());
		return mav;
	}
	
	@RequestMapping("/admin/provider/view")
	public ModelAndView view(String code){
		
		//System.out.println("코드:"+code);
		Provider provider  = providerDao.select(code);

		if(provider == null){
			throw new IllegalArgumentException("message");
		}
		
		ModelAndView mav = new ModelAndView("admin/provider/view");
		mav.addObject("provider", provider);
		return mav;
	}
	
	@RequestMapping("/admin/provider/list")
	public ModelAndView list(){
		List<Provider> providerList = providerDao.selectList(true);
		
		ModelAndView mav = new ModelAndView("admin/provider/list");
		mav.addObject("providerList", providerList);
		return mav;
	}
	
	@RequestMapping("/admin/provider/delete")
	public ModelAndView delete(String code){
		
		providerDao.delete(code);
		ModelAndView mav = new ModelAndView(new RedirectView("/admin/provider/list.html"));
		return mav;
	}
}
